package com.example.config;

import com.example.service.SpringSecuirtyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class MyWebSecurity extends WebSecurityConfigurerAdapter {
    @Autowired
    private SpringSecuirtyUserService springSecuirtyUserService;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        http.headers().frameOptions().sameOrigin();
        http.authorizeRequests()
                .antMatchers("/css").permitAll()
                .antMatchers("/img/**").permitAll()
                .antMatchers("/js/**").permitAll()
                .antMatchers("/plugins/**").permitAll()
                .antMatchers("/pages/login.html").permitAll()
                .anyRequest().authenticated();
        http
                .formLogin()//表单认证
                .loginProcessingUrl("/login")//登录路径 和表单一致
                .usernameParameter("username")
                .passwordParameter("password")
                .defaultSuccessUrl("/pages/main.html").permitAll();//成功跳转

        http.logout()//注销返回路径
                .logoutUrl("/logout");//返回注销路径
        http.csrf().disable();//关闭


    }
    /***
     * 自定义认证策略
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //super.configure(auth);
        auth.userDetailsService(springSecuirtyUserService);
    }
}
